<?php

class GetpwdForm extends CFormModel {

    public $email;
    public $authcode;
    public $mobile;
    public $types;
    public $uid;

    public function rules() {
        return array(
            array('email', 'required', 'on' => 'getpwdbyemail'),
            array('mobile,authcode,uid', 'required', 'on' => 'getpwdbymobile'),
            array('email', 'email'),
        );
    }

    /**
     * Declares attribute labels.
     */
    public function attributeLabels() {
        return array(
            'email' => '邮箱地址',
            'mobile' => '手机号码',
            'authcode' => '验证码',
        );
    }

    public function getpwdbyemail() {
        if (!$this->hasErrors()) {
            $api = new Api('account');
            $params = array(
                'UpdatePwdByEmailReq' => array(
                    'email' => $_POST['GetpwdForm']['email'],
                )
            );
            if (trim($_POST['GetpwdForm']['email'])) {
                $row = $api->apiclient('/LdAccountServer/userAccount/updatePwdByEmail', $params);
                if ($row == 0) {
                    throw new CHttpException(404, '网络异常,请稍后再试');
                }
                if ($row['code'] == 0) {
                    return true;
                } else {
                    $this->addError('email', $row['ExceptionRsp']['msg']);
                }
            } else {
                $this->addError('email', '邮箱不能为空');
            }
        }
    }

    public function getpwdbymobile() {
        if (!$this->hasErrors()) {
            $api = new Api('account');

            $params = array(
                'AuthCodeReq' => array(
                    'userId' => $_POST['GetpwdForm']['uid'],
                    'userClient' => $_POST['GetpwdForm']['mobile'],
                    'authCode' => md5($_POST['GetpwdForm']['authcode']),
                )
            );
            if (trim($_POST['GetpwdForm']['mobile']) && trim($_POST['GetpwdForm']['authcode'])) {
                $row = $api->apiclient('/LdAccountServer/userAccount/authCode', $params);
                if ($row == 0) {
                    throw new CHttpException(404, '网络异常,请稍后再试');
                }
                if ($row['code'] == 0) {
                    $flag['status'] = 1;
                    $flag['mobile'] = $_POST['GetpwdForm']['mobile'];
                    $flag['uid'] = $row['AuthCodeRsp']['userId'];
                    $flag['types'] = 2;

                    Yii::app()->session['getpwd'] = CJSON::encode($flag);
                    
                    return true;
                } else {
                    $this->addError('mobile', $row['ExceptionRsp']['msg']);
                }
            } else {
                $this->addError('mobile', '手机号码和验证码不能为空');
            }
        }
    }

}

?>
